package com.ntschy.web.controller.bt;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtPolicy;
import com.ntschy.bt.entity.dto.btPolicyDTO.BtPolicyAddUpdateDTO;
import com.ntschy.bt.entity.dto.btPolicyDTO.BtPolicyPageDTO;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyAuditStatusEnum;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyLevelEnum;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyStatusEnum;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyTypeEnum;
import com.ntschy.bt.entity.mapstruct.BtPolicyMapstruct;
import com.ntschy.bt.entity.valid.BtPolicyGroups;
import com.ntschy.bt.service.IBtPolicyService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/bt/policy")
@Api(tags = "政策发布接口")
@Validated
public class BtPolicyController extends BaseController {

    @Autowired
    private IBtPolicyService policyService;
    /**
     * 保存政策发布
     *
     * @param addUpdateDTO
     * @return
     */
    @ApiOperation(value = "保存政策发布", notes = "")
    @ApiImplicitParam(name = "addDTO", value = "保存政策发布实体类", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/addSavaPolicy")
    public AjaxResult<String> addSavaPolicy(@Validated(BtPolicyGroups.Add.class) @RequestBody BtPolicyAddUpdateDTO addUpdateDTO) {
        addUpdateDTO.setStatus(BtPolicyStatusEnum.TO_COMMIT);
        BtPolicy addbtPolicy = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(addUpdateDTO);
        return toAjax(policyService.addBtPolicy(addbtPolicy), "新增成功", "新增失败");
    }

    /**
     * 政策发布
     *
     * @param addSubmitDTO
     * @return
     */
    @ApiOperation(value = "政策发布", notes = "")
    @ApiImplicitParam(name = "submitDTO", value = "新增政策发布实体类", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/addSubmitPolicy")
    public AjaxResult<String> addSubmitPolicy(@Validated(BtPolicyGroups.Add.class) @RequestBody BtPolicyAddUpdateDTO addSubmitDTO) {
        if (addSubmitDTO.getStatus()==null|| addSubmitDTO.getStatus()==BtPolicyStatusEnum.TO_COMMIT
                && addSubmitDTO.getExamineResult()==null){
            addSubmitDTO.setStatus(BtPolicyStatusEnum.TO_CHECK);
            addSubmitDTO.setExamineResult(BtPolicyAuditStatusEnum.TO_WAIT);
        }
        BtPolicy addSubmitBtPolicy = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(addSubmitDTO);
        return toAjax(policyService.addBtPolicy(addSubmitBtPolicy),"发布成功","发布失败");
    }

    /**
     * 前端获取政策级别下拉框选项
     */
    @ApiOperation(value = "前端获取政策级别下拉框",notes = "")
    @PostMapping("/policyLevelInfo")
    public AjaxResult getPolicyLevelInfo(){
        AjaxResult levelOptions = new AjaxResult();
        List listOptions = new ArrayList();
        listOptions.add(BtPolicyLevelEnum.values());
        listOptions.add(BtPolicyTypeEnum.values());
        listOptions.add(BtPolicyStatusEnum.values());
        levelOptions.setData(listOptions);
        return AjaxResult.success("获取成功",listOptions);
    }

    /**
     * 政策发布列表信息
     */
    @ApiOperation(value = "政策发布列表信息", notes = "")
    @ApiImplicitParam(name = "listPageInfo", value = "政策发布列表信息", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/pageWaitSubmitPolicy")
    public  AjaxPageResult getListPolicyPageInfo(@RequestBody BtPolicyPageDTO pageDTO){
        Page<BtPolicy> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        BtPolicy queryBtPolicy = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.pageBtPolicy(pageDTO);
        List<BtPolicy> list = policyService.pageBtPolicy(queryBtPolicy);
       return buildPageResult(list,page);
    }

    /**
     * 查询政策详情信息  根据ID
     */
    @ApiOperation(value = "查询政策详情信息", notes = "")
    @ApiImplicitParam(name = "id", value = "查询政策详情信息", required = true, dataType = "BtPolicyPageDTO")
    @GetMapping ("/fatchWaitSubmitPolicy/{id}")
    public AjaxResult selectPolicyInfoById(@PathVariable String id){
        BtPolicy btPolicy1 = policyService.selectBtPolicy(id);
        return AjaxResult.success("查询成功",btPolicy1);
    }

    /**
     * 修改 更新政策信息
     */
    @ApiOperation(value = "修改 更新政策信息", notes = "")
    @ApiImplicitParam(name = "BtPolicy", value = "修改 更新政策信息", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/updatePolicy")
    public AjaxResult updateBtPolicy(@RequestBody BtPolicyAddUpdateDTO btPolicy){
        BtPolicy selectBtPolicy = policyService.selectBtPolicy(btPolicy.getId());
        if (btPolicy.getExamineResult() == null && selectBtPolicy.getStatus()==BtPolicyStatusEnum.TO_COMMIT){
            BtPolicy btPolicy1 = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(btPolicy);
            return toAjax(policyService.updateBtPolicy(btPolicy1),"修改成功","修改失败");
        }
        if (btPolicy.getExamineResult()!=null){
            btPolicy.setStatus(BtPolicyStatusEnum.TO_CHECK);
            btPolicy.setExamineResult(BtPolicyAuditStatusEnum.TO_WAIT);
            BtPolicy btPolicy2 = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(btPolicy);
            return toAjax(policyService.updateBtPolicy(btPolicy2),"发布成功","发布失败");
        }
        BtPolicy btPolicy2 = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(btPolicy);
        return toAjax(policyService.updateBtPolicy(btPolicy2),"发布成功","发布失败");
    }

    /**
     * 删除 删除政策信息
     */
    @ApiOperation(value = "删除政策信息", notes = "")
    @ApiImplicitParam(name = "BtPolicy", value = "删除政策信息", required = true, dataType = "BtPolicyPageDTO")
    @GetMapping("/deletePolicy")
    public AjaxResult deletePolicyById(String id ){
        int i = policyService.deleteBtPolicy(id);
        return toAjax(i,"删除成功","删除失败");
    }
}
